package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.model.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.vo.GoodsDescVo;
import com.dongdongshop.vo.GoodsVo;
import com.dongdongshop.vo.ItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;


    @Override
    @Transactional
    public void add(GoodsVo goodsVo) {

        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        tbGoodsMapper.insertSelective(tbGoods);

        GoodsDescVo goodsDescVo = goodsVo.getGoodsDescVo();
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        goodsDescVo.setGoodsId(tbGoods.getId());
//        BeanUtils.copyProperties(tbGoodsDesc, goodsDescVo);
        BeanUtils.copyProperties(goodsDescVo, tbGoodsDesc);
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);

        List<ItemVo> itemVos = goodsVo.getItemVos();
        itemVos.stream().forEach(c->{
            TbItem tbItem=new TbItem();
            BeanUtils.copyProperties(c, tbItem);
            tbItem.setGoodsId(tbGoodsDesc.getGoodsId());
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            tbItem.setSeller(tbGoods.getSellerId());
            tbItem.setSellerId(tbGoods.getSellerId());
            tbItemMapper.insertSelective(tbItem);
        });

    }

    @Override
    public List<GoodsVo> getAll() {
        List<TbGoods> goods = tbGoodsMapper.selectByExample(null);
        List<GoodsVo> goodsVOList = JSONObject.parseArray(JSONObject.toJSONString(goods),GoodsVo.class);
        return goodsVOList;
    }

    @Override
    public int deleteBatch(Long[] ids) {
        TbGoodsExample example = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = example.createCriteria();
        List<Long> list = Arrays.asList(ids);
        criteria.andIdIn(list);
        return tbGoodsMapper.deleteByExample(example);
    }

    @Override
    public GoodsVo getGoodsDetail(Long goodsId) {
        //根据id查询商品数据
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        GoodsVo goodsVO = new GoodsVo();
        BeanUtils.copyProperties(tbGoods,goodsVO);
        //根据goodsId查询goodsDesc
        TbGoodsDesc tbGoodsDesc = tbGoodsDescMapper.selectByPrimaryKey(goodsId);
        GoodsDescVo goodsDescVO = new GoodsDescVo();
        BeanUtils.copyProperties(tbGoodsDesc,goodsDescVO);
        goodsVO.setGoodsDescVo(goodsDescVO);

        //根据goodsId查询item表中的数据
        TbItemExample example = new TbItemExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId);
        List<TbItem> itemList = tbItemMapper.selectByExample(example);
        List<ItemVo> itemVOList = itemList.stream().map(item -> {
            ItemVo vo = new ItemVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());

        //将sku集合存放到goodsVo中
        goodsVO.setItemVos(itemVOList);
        return goodsVO;
    }


    @Override
    public void updateAuditStatus(List<Long> ids, String status) {
        TbGoodsExample example = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);

        TbGoods tbGoods = new TbGoods();
        if("1".equals(status)){
            tbGoods.setAuditStatus(status);
            //审核通过 发送短信  dongdongshop_freemarker_service
        }
        if("2".equals(status)){
            tbGoods.setAuditStatus(status);
        }
        tbGoodsMapper.updateByExampleSelective(tbGoods,example);
    }

//    @Override
//    public GoodsVo getOne(Long id) {
//        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
//        TbGoodsDesc tbGoodsDesc = tbGoodsDescMapper.selectByPrimaryKey(id);
//        GoodsVo goodsVo = new GoodsVo();
//        GoodsDescVo descVo = new GoodsDescVo();
//        BeanUtils.copyProperties(tbGoods,goodsVo);
//        BeanUtils.copyProperties(tbGoodsDesc,descVo);
//        TbItem tbItem = tbItemMapper.selectByPrimaryKey(536563L);
//        ItemVo vo = new ItemVo();
//        BeanUtils.copyProperties(tbItem,vo);
//        goodsVo.setGoodsDescVo(descVo);
//        goodsVo.setItemVos(vo);
//        return goodsVo;
//    }
    //增加购物车 根据商品Id去查询商品的数据

    @Override
    public ItemVo getItem(Long itemId) {
        TbItem item = tbItemMapper.selectByPrimaryKey(itemId);
        ItemVo vo =  new ItemVo();
        BeanUtils.copyProperties(item, vo);
        return vo;
    }

}
